package top.infopub.mgr.la.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.la.dao.CarryLawcaseAttDao;
import top.infopub.mgr.la.dao.CarryLawcaseDao;
import top.infopub.mgr.la.domain.CarryLawcaseDomain;
import top.infopub.mgr.la.service.CarryLawcaseService;
import top.infopub.mgr.la.vo.CarryLawcaseVo;
import top.infopub.model.la.CarryLawcase;
import top.infopub.model.la.CarryLawcaseAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class CarryLawcaseServiceImpl extends GenericServiceImpl<CarryLawcase, String> implements
		CarryLawcaseService {

	@Autowired
	private CarryLawcaseDao carryLawcaseDao;
	
	@Autowired
    private CarryLawcaseAttDao carryLawcaseAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<CarryLawcase, String> getDao() {
		return carryLawcaseDao;
	}

    @Override
    public Page<CarryLawcaseDomain> selectByDomainWhere(Page<CarryLawcaseDomain> page,
                                                        CarryLawcaseVo carryLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<CarryLawcaseDomain>)carryLawcaseDao.selectByDomainWhere(carryLawcaseVo);
    }

    @Override
    public Page<CarryLawcaseDomain> selectMyCheckByDomainWhere(Page<CarryLawcaseDomain> page,
                                                               CarryLawcaseVo carryLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<CarryLawcaseDomain>)carryLawcaseDao.selectMyCheckByDomainWhere(carryLawcaseVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCarryLawcase(CarryLawcaseVo carryLawcase,
                                 List<CarryLawcaseAtt> carryLawcaseAtts) {
        carryLawcaseDao.insertById(carryLawcase);
        
        if(CollectionUtils.isNotEmpty(carryLawcaseAtts)){
            carryLawcaseAttDao.insertByBatch(carryLawcaseAtts);
        }
        
        // 1000万
        BigDecimal cmp = new BigDecimal("10000000");
        String srcBizTable = "LA_CARRY_LAWCASE";
        String branchCode_lt = "target_amount<=1000万";
        String branchCode_gt = "target_amount>1000万";
        String templateId = "";
        // <=1000万
        if (carryLawcase.getTargetAmount().compareTo(cmp) <= 0) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_lt);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_gt);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, carryLawcase.getId(),
            carryLawcase.getProcessId(), null);
    }

    @Override
    public CarryLawcaseVo queryById(String id) {
        return carryLawcaseDao.selectById(id);
    }

    @Override
    public List<CarryLawcase> selectUsablePeace() {
        return carryLawcaseDao.selectUsablePeace();
    }

}